package com.xscha.tableforge.constants;

/**
 * 字段类型枚举
 */
public enum FieldType {
    STRING("STRING", "字符串"),
    INTEGER("INTEGER", "整数"),
    DECIMAL("DECIMAL", "小数"),
    BOOLEAN("BOOLEAN", "布尔值"),
    DATE("DATE", "日期"),
    DATETIME("DATETIME", "日期时间"),
    TIME("TIME", "时间"),
    REFERENCE("REFERENCE", "引用"),
    NUMBER("NUMBER", "数字"),
    SINGLE_SELECT("SINGLE_SELECT", "单选"),
    RADIO("RADIO", "RADIO单选"),
    MULTI_SELECT("MULTI_SELECT", "多选"),
    TAGS("TAGS", "标签"),
    // 新增关联类型
    RELATION_ONE_TO_ONE("RELATION_ONE_TO_ONE", "一对一关系"),
    RELATION_ONE_TO_MANY("RELATION_ONE_TO_MANY", "一对多关系"),
    RELATION_MANY_TO_ONE("RELATION_MANY_TO_ONE", "多对一关系"),
    RELATION_MANY_TO_MANY("RELATION_MANY_TO_MANY", "多对多关系");

    private final String code;
    private final String description;

    /**
     * 判断是否为单选类型字段
     *
     * @return 是否为单选类型
     */
    public boolean isSingleSelect() {
        return this == SINGLE_SELECT || this == RADIO;
    }

    /**
     * 判断是否为多选类型字段
     *
     * @return 是否为多选类型
     */
    public boolean isMultiSelect() {
        return this == MULTI_SELECT || this == TAGS;
    }
    
    /**
     * 判断是否为关联类型字段
     *
     * @return 是否为关联类型
     */
    public boolean isRelation() {
        return this == RELATION_ONE_TO_ONE || this == RELATION_ONE_TO_MANY || 
               this == RELATION_MANY_TO_ONE || this == RELATION_MANY_TO_MANY;
    }

    FieldType(String code, String description) {
        this.code = code;
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 根据字段类型代码获取枚举值
     *
     * @param code 字段类型代码
     * @return 对应的枚举值，如果不存在则返回null
     */
    public static FieldType fromCode(String code) {
        if (code == null) {
            return null;
        }

        for (FieldType fieldType : values()) {
            if (fieldType.getCode().equalsIgnoreCase(code)) {
                return fieldType;
            }
        }

        return null;
    }
}